﻿/* 
 * Change log
 * 1.0.0, 19 MAY 2014 - Initial release.
 * 
 * 1.0.4, 30 MAY 2014
 * - Made ID property editable, but not (by default) visible in Lightswitch
 *   (this is to allow the creator of a FileNode to specify its ID)
 * - Made IsFolder editable (this is to allow callers to more easily create
 *   folders; it is still recommended that this field be displayed as read
 *   only if displayed for an existing FileNode)
 * - Made Name required and set maximum length of Name to 255 characters
 * - Turned off display of the Data field on forms by default
 * 
 * 1.0.5, 29 SEP 2014
 * - Added a new property FullPath to make it easier to connect with the 
 *   UNC share hosted by SQL Server for a FileTable
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.ComponentModel.DataAnnotations;

namespace SqlFileTableExtension
{
    #region FileNode class

    /// <summary>
    /// An abstract representation of one node within a FileTable, representing
    /// one file or one folder
    /// </summary>
    public class FileNode
    {
        /// <summary>
        /// The stream_id field from the FileTable
        /// </summary>
        [Key]
        [Display(Description = "The unique identifier of this file or folder")]
        //[Editable(false)] - changed in 1.0.4
        [ScaffoldColumn(false)] // added in 1.0.4
        public Guid ID { get; set; }

        /// <summary>
        /// The name field from the FileTable
        /// </summary>
        [Required(AllowEmptyStrings=false)] // added in 1.0.4
        [StringLength(255)] // added in 1.0.4
        [Display(Description = "The name of the file or folder")]
        public string Name { get; set; }

        /// <summary>
        /// The file_type field from the FileTable;
        /// cannot be set; calculated based on Name
        /// </summary>
        [Display(Description = "The file type (such as DOC, TXT, or PDF) - or blank for folders")]
        [Editable(false)]
        public string FileType { get; set; }

        /// <summary>
        /// A string representing the relative path to the file or folder 
        /// represented by this node, starting from the datbase share root,
        /// including the name of this file or folder, beginning with the 
        /// FileTable folder name; cannot be set directly (see ParentPathLocator)
        /// </summary>
        /// <example>
        /// Assuming that "docs" is the folder representing the FileTable itself: 
        /// \docs\Containing Folder\File.txt (for a file)
        /// \docs\New Folder (for a folder)
        /// </example>
        [Display(Description = "The relative path from the top of the FileTable to this file or folder")]
        [Editable(false)]
        public string Path { get; set; }

        /// <summary>
        /// A string representing the absolute path to the file or folder 
        /// represented by this node, starting from the NETBIOS server name,
        /// including the name of this file or folder; cannot be set directly 
        /// (see ParentPathLocator)
        /// </summary>
        /// <example>
        /// Assuming that "docs" is the folder representing the FileTable itself,
        /// SERVER is the server name, SqlExpress is the instance name,
        /// and Files is the FileTable filespace name: 
        /// \\SERVER\SqlExpress\Files\docs\Containing Folder\File.txt (for a file)
        /// \\SERVER\SqlExpress\Files\docs\New Folder (for a folder)
        /// </example>
        [Display(Description = "The absolute path to this file or folder starting at the server name")]
        [Editable(false)]
        public string FullPath { get; set; }

        /// <summary>
        /// The size of the file in bytes (always zero for folders)
        /// </summary>
        [Display(Description = "The size of the file in bytes (always zero for folders)")]
        [Editable(false)]
        public long Size { get; set; }

        /// <summary>
        /// The file content expressed as a byte array; may be zero-length 
        /// or null, if the query method invoked doesn't specifically request
        /// the file content; always null for folders
        /// </summary>
        [Display(Description = "The file content (always empty for folders)")]
        [ScaffoldColumn(false)] // added in version 1.0.4
        public byte[] Data { get; set; }

        /// <summary>
        /// True if this node represents a folder; false if it represents a file
        /// </summary>
        [Display(Description = "True if this item represents a folder")]
        //[Editable(false)] - changed in 1.0.4
        public bool IsFolder { get; set; }

        /// <summary>
        /// The path_locator value for this File or Folder in the FileTable, 
        /// as set by the database internally; cannot be modified directly
        /// </summary>
        /// <remarks>
        /// The PathLocator property contains the value of path_locator from the
        /// FileTable schema, and should not be set by a consuming application.
        /// 
        /// The ParentPathLocator property may be set, upon either creation of a
        /// new FileNode, or update of an existing FileNode (see ParentPathLocator).
        /// </remarks>
        [Editable(false)]
        [ScaffoldColumn(false)] // added in 1.0.4
        [Display(Description = "The internal path locator of this item in the database")]
        public string PathLocator { get; set; }

        /// <summary>
        /// The FileTable path_locator for this node's parent node in the FileTable
        /// </summary>
        /// <remarks>
        /// For existing nodes, this value is retrieved from the parent_path_locator
        /// column in the FileTable.  That column is a persisted computed column,
        /// and cannot be modified.
        /// 
        /// Setting this property to NULL (or leaving it NULL) will:
        /// - Create Files or Folders at the root of the FileTable hierarchy
        /// - Move existing Files or Folders to the root of the FileTable hierarchy
        /// 
        /// Setting this property to a non-NULL value will:
        /// - Create new Files and Folders underneath the Folder with the specified
        /// path_locator in the database
        /// - Move existing Files and Folders underneath the Folder with the specified
        /// path_locator in the database (once the change is saved to the database)
        /// 
        /// Changing this value is the only way to create or move Files or Folders in a 
        /// specific location in the FileTable, other than the root of the FileTable.
        /// </remarks>
        [ScaffoldColumn(false)] // added in 1.0.4
        [Display(Description = "The internal path locator of this item's parent in the database")]
        public string ParentPathLocator { get; set; }

    }

    #endregion
}
